package com.lovo.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.config.AppConfig;
import com.lovo.model.UserDO;
import com.lovo.service.IUserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/14
 */
public class LoginServlet extends HttpServlet {
    private IUserService service;
    private ObjectMapper objectMapper;
    @Override
    public void init() throws ServletException {
        //创建spring容器, 注解配置器对象
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        this.service = (IUserService) context.getBean("userService");
        this.objectMapper = (ObjectMapper) context.getBean("objectMapper");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        UserDO login = service.login(username, pwd);
        if (login != null) {

            resp.getWriter().println(true);
        }else {

            resp.getWriter().println(false);
        }

    }
}
